const pub = {
    // 将ID、ParentID这种数据格式转换为树格式
    arrayToTree(data, id, pid) {      // 将ID、ParentID这种数据格式转换为树格式
        if (!data || !data.length) return [];
        let targetData = [];                    // 存储数据的容器(返回)
        let records = {};
        let itemLength = data.length;           // 数据集合的个数
        for (let i = 0; i < itemLength; i++) {
            let o = data[i];
            records[o[id]] = o;
        }
        for (let i = 0; i < itemLength; i++) {
            let currentData = data[i];

            currentData.open = false;
            if (sessionStorage.getItem('currentPid') === currentData.id) {
                currentData.open = true;
            }

            let parentData = records[currentData[pid]];
            if (!parentData && currentData.is_menu === '1') {
                targetData.push(currentData);
                continue;
            }
            // parentData.open = false;
            if (currentData.is_menu === '1') {
                parentData.children = parentData.children || [];
                parentData.children.push(currentData);
            }
        }
        return targetData;
    },

    // 将ID、ParentID这种数据格式转换为树格式(显示组织架构按钮权限操作)
    arrayToTreeList(data, id, pid) {      // 将ID、ParentID这种数据格式转换为树格式
        if (!data || !data.length) return [];
        let targetData = [];                    // 存储数据的容器(返回)
        let records = {};
        let itemLength = data.length;           // 数据集合的个数
        for (let i = 0; i < itemLength; i++) {
            let o = data[i];
            records[o[id]] = o;
        }
        for (let i = 0; i < itemLength; i++) {
            let currentData = data[i];
            currentData.open = true;
            let parentData = records[currentData[pid]];
            if (!parentData) {
                targetData.push(currentData);
                continue;
            }
            // parentData.open = false;
            parentData.children = parentData.children || [];
            parentData.children.push(currentData);
        }
        return targetData;
    },

    allMenu: [],

    loadopen() {
        this.loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.2)'
        });
        setTimeout(() => {
            this.loading.close();
        }, 5000);
    },

    loadclose() {
        this.loading.close();
    },

    // 获取当前时间格式化的字符串
    getCurrentTime(timeStr) {
        function g(t) {
            if (t < 10) {
                t = '0' + t;
            }
            return t;
        }

        Date.prototype.toLocaleString = function () {
            return this.getFullYear() + '-' + g(this.getMonth() + 1) + '-' + g(this.getDate())
                + ' ' + g(this.getHours()) + ':' + g(this.getMinutes()) + ':' + g(this.getSeconds());
        };
        let str = timeStr ? timeStr : new Date().getTime();
        let time = new Date(str);
        time = time.toLocaleString();
        return time;
    },
};

export default pub;
